package com.boomsecret.juc.collections;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wangjinliang on 2019/1/22.
 */
public class ConcurrentHashMapDemo {
    private static final ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        // CAS + 链表 (链表长度足够长会转成红黑树)
        concurrentHashMap.put("bb", "bb");
        concurrentHashMap.put("cc", "cc");
        concurrentHashMap.put("aa", "aa");
        concurrentHashMap.put("dd", "dd");
        print(concurrentHashMap);
    }

    private static void print(ConcurrentHashMap<String, String> concurrentHashMap) {
        Iterator<Entry<String, String>> iterator = concurrentHashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> next = iterator.next();
            System.out.println("key:" + next.getKey() + " value:" + next.getValue());
        }
    }
}